//
//  XMHistoryCollectionViewCell.m
//  MyNewProject
//
//  Created by apple on 2019/1/27.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "XMHistoryCollectionViewCell.h"
#import "OYCountDownManager.h"
@implementation XMHistoryCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self)
    {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
        [self addSubview:self.cellBackView];
        [_cellBackView addSubview:self.productImageView];
        [_cellBackView addSubview:self.clearBackView];

        [_cellBackView addSubview:self.timeLabel];
        [self addSubview:self.countDownTimelab];

        [_cellBackView addSubview:self.productName];
        [_cellBackView addSubview:self.hammerPrice];
        [_cellBackView addSubview:self.supplier];
        [_cellBackView addSubview:self.integralLabel];
    }
    return self;
}
-(UILabel *)countDownTimelab{
    if (!_countDownTimelab) {
        _countDownTimelab = [self makeCustomLabel];
        _countDownTimelab.hidden = YES;
    }
    return _countDownTimelab;
}
- (UILabel *)makeCustomLabel {
    UILabel *label = [[UILabel alloc] init];
    label.frame = self.timeLabel.frame;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = LightFont(12);
    return label;
}
#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (self.dataModel.status == 1 && [self.dataModel.lost_begin_time integerValue] > 0) {//未开始
        NSInteger countDown = [self.dataModel.lost_begin_time integerValue] - 1;
        self.dataModel.lost_begin_time = [NSString stringWithFormat:@"%ld",[self.dataModel.lost_begin_time integerValue] - 1];
        /// 当倒计时到了进行回调
        if (countDown <= 0) {
            NSInteger endDown =  [self.dataModel.lost_end_time integerValue];//直接变成距结束
            self.dataModel.status = 2;
            self.countDownTimelab.text = [NSString stringWithFormat:@"距结束：%02zd时%02zd分%02zd秒", endDown/3600, (endDown/60)%60, endDown%60];
            // 回调给控制器
            if (self.countDownZero) {
                self.countDownZero(self.dataModel);
            }
            return;
        }
        /// 重新赋值
        self.countDownTimelab.text = [NSString stringWithFormat:@"距开始：%02zd时%02zd分%02zd秒", countDown/3600, (countDown/60)%60, countDown%60];
    }else if (self.dataModel.status == 2  && [self.dataModel.lost_end_time integerValue] > 0) {//距结束
        NSInteger countDown =  [self.dataModel.lost_end_time integerValue] - 1;
        self.dataModel.lost_end_time = [NSString stringWithFormat:@"%ld",[self.dataModel.lost_end_time integerValue] - 1];
        /// 当倒计时到了进行回调
        if (countDown <= 0) {
            self.timeLabel.text = @"已结束";
            self.dataModel.status = 3;
            self.timeLabel.hidden = NO;
            self.countDownTimelab.hidden = YES;
            // 回调给控制器
            if (self.countDownZero) {
                self.countDownZero( self.dataModel);
            }
            return;
        }
        /// 重新赋值
        self.countDownTimelab.text = [NSString stringWithFormat:@"距结束：%02zd时%02zd分%02zd秒", countDown/3600, (countDown/60)%60, countDown%60];
    }
   
}
-(void)LoadProductInfomation:(XM_AuctionProductModel *)model isShop:(BOOL)shop{
    self.dataModel = model;
    self.supplier.text = [NSString stringWithFormat:@"供应商:%@",model.supplier];
    self.productName.text = model.title;
    self.supplier.text = [NSString stringWithFormat:@"供应商:%@",model.supplier];
    self.hammerPrice.hidden  = shop;
    self.clearBackView.hidden = shop;
    _clearBackView.backgroundColor = [UIColor blackColor];
    _clearBackView.alpha = 0.5;
    self.integralLabel.hidden = !shop;
    [self.productImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:PlaceholderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
    }];
    switch (model.status) {
        case 1:{
            self.timeLabel.text = @"未开始";
        }
            break;
        case 2:{
            self.timeLabel.text = @"进行中";
            self.clearBackView.backgroundColor = SKColorWithHex(0xFF2424);
            self.clearBackView.alpha = 0.8;
        }
            break;
        case 3:{
            self.timeLabel.text = @"已结束";
        }
            break;
        case 4:{
            self.timeLabel.text = @"已成交";
        }
            break;
        default:
            break;
    }
    if (shop) {
        if ([model.typeName isEqualToString:@"MallLifeProductType"]||[model.typeName isEqualToString:@"MallLikeProductType"]) {
            self.integralLabel.text = [NSString stringWithFormat:@"现价：%@   销量：%@",model.price,model.sales];

        }
        else
        {
            self.integralLabel.text = [NSString stringWithFormat:@"积分：%@   销量：%@",model.jifen,model.sales];

        }
        
    }else{
        self.hammerPrice.text = [NSString stringWithFormat:@"现价：%@",model.the_new_price];
    }
    // 手动刷新数据
    
    self.timeLabel.hidden = ((model.status == 1  && [model.lost_begin_time integerValue] > 0) || (model.status == 2  && [model.lost_end_time integerValue] > 0));//未开始 并且有倒计时    进行中 并且有倒计时
    self.countDownTimelab.hidden = !self.timeLabel.hidden;
    [self countDownNotification];
    
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(UIView *)cellBackView{
    if (!_cellBackView) {
        _cellBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _cellBackView.backgroundColor = [UIColor whiteColor];
        _cellBackView.layer.cornerRadius = 5;
        _cellBackView.clipsToBounds = YES;
        _cellBackView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _cellBackView.layer.borderWidth = 1.0f;
    }
    return _cellBackView;
}
-(UIImageView *)productImageView{
    if (!_productImageView) {
        _productImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _cellBackView.frame.size.width, _cellBackView.frame.size.width)];
        _productImageView.clipsToBounds = YES;
    }
    return _productImageView;
}
-(UIView *)clearBackView{
    if (!_clearBackView) {
        _clearBackView = [[UIView alloc]initWithFrame:CGRectMake(0, _productImageView.height - 20, _cellBackView.width, 20)];
        _clearBackView.backgroundColor = [UIColor blackColor];
        _clearBackView.alpha = 0.5;
    }
    return _clearBackView;
}
-(UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _productImageView.height - 20, _cellBackView.width, 20)];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.hidden = YES;
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.font = [UIFont systemFontOfSize:12];
    }
    return _timeLabel;
}
-(UILabel *)productName{
    if (!_productName) {
        _productName = [[UILabel alloc]initWithFrame:CGRectMake(10,_cellBackView.frame.size.width, _cellBackView.frame.size.width - 20, 40)];
        _productName.numberOfLines = 2;
        _productName.textColor = [UIColor darkTextColor];
        _productName.font = [UIFont systemFontOfSize:15];
    }
    return _productName;
}
-(UILabel *)hammerPrice{
    if (!_hammerPrice) {
        _hammerPrice = [[UILabel alloc]initWithFrame:CGRectMake(10,_cellBackView.frame.size.width + 40, _cellBackView.width - 20, 20)];
        _hammerPrice.textColor = [UIColor darkGrayColor];
        _hammerPrice.font = [UIFont systemFontOfSize:13];
    }
    return _hammerPrice;
}

-(UILabel *)supplier{
    if (!_supplier) {
        _supplier = [[UILabel alloc]initWithFrame:CGRectMake(10,_cellBackView.frame.size.width + 56, _cellBackView.width - 20, 20)];
        _supplier.textColor = [UIColor darkGrayColor];
        _supplier.font = [UIFont systemFontOfSize:13];
    }
    return _supplier;
}
-(UILabel *)integralLabel{
    if (!_integralLabel) {
        _integralLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,_cellBackView.frame.size.width + 40, _cellBackView.width - 20, 20)];
        _integralLabel.hidden = YES;
        _integralLabel.textColor = [UIColor blackColor];
        _integralLabel.font = [UIFont systemFontOfSize:13];
    }
    return _integralLabel;
}
@end

